package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.entity.Training;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.TrainingService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
public class TrainingController {

    @Resource
    TrainingService trainingService;

    // 条件查询
    @PostMapping("/training/{pageIndex}/{pageSize}")
    public ResponseText getTraining(@PathVariable("pageIndex")int pageIndex,
                                    @PathVariable("pageSize")int pageSize,
                                    @RequestBody Training training){

        ResponseText<PageBean<Training>> responseText = null;
        try{
            PageBean<Training> allTrainingByCondition = trainingService.getAllTrainingByCondition(training, pageIndex, pageSize);
            responseText = new ResponseText<>(allTrainingByCondition,200,"查询成功!!!");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }

        return responseText;
    }

    // 删除培训
    @DeleteMapping("/training/{tid}")
    public ResponseText deleteTraining(@PathVariable("tid")int tid){

        ResponseText<PageBean<Training>> responseText = null;

        try {

            trainingService.deleteTraining(tid);
            responseText = new ResponseText<>(null,200,"删除成功！！！");

        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }

        return responseText;
    }

    // 添加培训
    @PostMapping("/training")
    public ResponseText addTraining(@RequestBody Training training){

        ResponseText responseText = null;
        try {
            trainingService.addTraining(training);
            responseText = new ResponseText(null,200,"添加成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 修改培训
    @PutMapping("/training")
    public ResponseText updateTraining(@RequestBody Training training){
        ResponseText responseText = null;
        try {
            trainingService.updateTraining(training);
            responseText = new ResponseText(null,200,"添加成功！！！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
